package com.atguigu.edu.service.impl;

import com.atguigu.base.ExceptionHnadler.CustomException;
import com.atguigu.edu.entity.frontvo.CourseFrontVo;
import com.atguigu.edu.entity.frontvo.CourseWebVo;
import com.atguigu.edu.client.VodClientService;
import com.atguigu.edu.entity.Course;
import com.atguigu.edu.entity.CourseDescription;
import com.atguigu.edu.entity.vo.CourseInfoVo;
import com.atguigu.edu.entity.vo.CoursePublishVo;
import com.atguigu.edu.entity.vo.CourseQuery;
import com.atguigu.edu.mapper.CourseMapper;
import com.atguigu.edu.service.ChapterService;
import com.atguigu.edu.service.CourseDescriptionService;
import com.atguigu.edu.service.CourseService;
import com.atguigu.edu.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ld
 * @since 2022-08-16
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Resource
    private CourseDescriptionService courseDescriptionService;
    @Resource
    private ChapterService chapterService;
    @Resource
    private VideoService videoService;
    @Resource
    private VodClientService vodClientService;
    @Override
    public String addCourseInfo(CourseInfoVo courseInfoVo) {
        Course course=new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        int insert = baseMapper.insert(course);
        if(insert==0){
            throw new CustomException(20001,"添加课程信息失败");
        }
        String cid = course.getId();
        CourseDescription courseDescription=new CourseDescription();
        courseDescription.setId(cid);
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.save(courseDescription);

        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        Course course = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        BeanUtils.copyProperties(course,courseInfoVo);
        CourseDescription courseDescription = courseDescriptionService.getById(courseId);
        String description = courseDescription.getDescription();
        courseInfoVo.setDescription(description);
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        Course course=new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        int i = baseMapper.updateById(course);
        if(i==0){
            throw new CustomException(20001,"更新课程信息失败");
        }
        CourseDescription courseDescription=new CourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        boolean b = courseDescriptionService.updateById(courseDescription);
        if(!b){
            throw  new CustomException(20001,"更新课程简介失败");
        }

    }
    //获取最终发布的课程信息
    @Override
    public CoursePublishVo getCoursePublishInfo(String courseId) {
        CoursePublishVo coursePublishInfo = baseMapper.getCoursePublishInfo(courseId);
        return coursePublishInfo;
    }


    @Override
    public Page pageQuery(Integer page, Integer size, CourseQuery courseQuery) {
        Page<Course> pageInfo=new Page<>(page,size);
        LambdaQueryWrapper<Course> queryWrapper=new LambdaQueryWrapper<>();
        if(courseQuery!=null) {
            String title = courseQuery.getTitle();
            String status = courseQuery.getStatus();
            String teacherId = courseQuery.getTeacherId();
            String subjectParentId = courseQuery.getSubjectParentId();
            String subjectId = courseQuery.getSubjectId();
            queryWrapper.like(!StringUtils.isEmpty(title), Course::getTitle, title)
                    .eq(!StringUtils.isEmpty(status), Course::getStatus, status)
                    .eq(!StringUtils.isEmpty(teacherId), Course::getTeacherId, teacherId)
                    .eq(!StringUtils.isEmpty(subjectParentId), Course::getSubjectParentId, subjectParentId)
                    .eq(!StringUtils.isEmpty(subjectId), Course::getSubjectId, subjectId);
        }
        queryWrapper.orderByDesc(Course::getGmtCreate);
        baseMapper.selectPage(pageInfo, queryWrapper);
        return pageInfo;
    }

    @Override
    public boolean removeCourse(String courseId) {
        //根据课程id删除小节
        videoService.removeVideoByCourseId(courseId);
        //根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);
        //根据课程id删除描述
        courseDescriptionService.removeById(courseId);
        //根据课程id删除课程本身
        int i = baseMapper.deleteById(courseId);

        return i>0;
    }
    //得到前台首页course数据
    @Cacheable(value = "course",key = "'getIndexCourses'")
    @Override
    public List<Course> getIndexCourse() {
        LambdaQueryWrapper<Course> queryWrapperCourse=new LambdaQueryWrapper<>();
        queryWrapperCourse.orderByDesc(Course::getId)
                .last("limit 8");
        List<Course> courseList = baseMapper.selectList(queryWrapperCourse);
        return courseList;
    }
    //前台课程分页展示
    @Override
    public Map<String, Object> getListByPage(Page<Course> pageInfo, CourseFrontVo courseFrontVo) {
        LambdaQueryWrapper<Course> queryWrapper=new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo)){
            String subjectParentId = courseFrontVo.getSubjectParentId();
            String subjectId = courseFrontVo.getSubjectId();
            String buyCountSort = courseFrontVo.getBuyCountSort();
            String gmtCreateSort = courseFrontVo.getGmtCreateSort();
            String priceSort = courseFrontVo.getPriceSort();

            queryWrapper.eq(!StringUtils.isEmpty(subjectParentId),Course::getSubjectParentId,subjectParentId)
                    .eq(!StringUtils.isEmpty(subjectId),Course::getSubjectId,subjectId)
                    .orderByDesc(!StringUtils.isEmpty(buyCountSort),Course::getBuyCount)
                    .orderByDesc(!StringUtils.isEmpty(gmtCreateSort),Course::getGmtCreate)
                    .orderByDesc(!StringUtils.isEmpty(priceSort),Course::getPrice)
                    .orderByDesc(Course::getId);

        }
        baseMapper.selectPage(pageInfo,queryWrapper);

        Map<String,Object> map=new HashMap<>();
        List<Course> records = pageInfo.getRecords();
        long current = pageInfo.getCurrent();
        long size = pageInfo.getSize();
        long pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        boolean hasPrevious = pageInfo.hasPrevious();
        boolean hasNext = pageInfo.hasNext();

        map.put("records",records);
        map.put("current",current);
        map.put("size",size);
        map.put("pages",pages);
        map.put("total",total);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);

        return map;
    }

    @Override
    public CourseWebVo getCourseWebInfo(String courseId) {

        return baseMapper.getCourseWebVoById(courseId);
    }
}
